/**
 * 
 */
package com.hezhi.framework.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.hezhi.trafficcopsys.domain.User;

/**
 * 从json文件读取数据，形成JSon对象，读取对象数据并打印
 * 
 * @author wudo
 * 
 */
public class JSONParser {

	public static void main(String[] args) {

		// 从JSon文件读取数据
		StringBuffer stringBuffer = new StringBuffer();
		String line = null;

		try {
			BufferedReader br = new BufferedReader(new FileReader(new File("users.json")));
			while ((line = br.readLine()) != null) {
				stringBuffer.append(line);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		try {
			// 将Json文件数据形成JSONObject对象
			JSONObject jsonObject = JSONObject.fromObject(stringBuffer.toString());

			// 获取JSONObject对象数据并打印
			JSONArray jsonArray = jsonObject.getJSONArray("provinces");

			User[] users = (User[]) JSONArray.toArray(jsonArray, User.class);
			
			for( User user : users){
				System.out.println(user.getUsername());
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
